import {createRouter, createWebHashHistory,createWebHistory} from 'vue-router'//加createWebHistory
import {ElMessage} from "element-plus";

const router = createRouter({
    history: createWebHistory(),//改 原createWebHashHistory()
    routes: [
        {
            path: "/",
            redirect: "/home"
        },
        {
            path: "/home",
            component: () => import("@/views/Home/Home.vue")
        },
        {
            path: "/discover",
            component: () => import("@/views/Discover/Discover.vue")
        },
        {
            path: "/weather",
            component: () => import("@/views/Weather/Weather.vue")
        },
        {
            path: "/user",
            // component: () => import("@/views/Login.vue")
            children: [
                {
                    path: "login",
                    component: () => import("@/views/Login/Login.vue")
                },
                {
                    path: "register",
                    component: () => import("@/views/Login/Login.vue"),
                    // beforeEnter: (to, from,next) => {
                    // reject the navigation
                    // console.log(666)
                    // next()
                    // return {
                    //         path: to.path,
                    //         query: {
                    //             wantRegister:true
                    //         },
                    //     }
                    // },
                }
            ]
        },
        {
            path: "/heat",
            component: () => import("@/views/Heat/Heat.vue")
        },
        {
            path: "/meteorology",
            component: () => import("@/views/Meteorology/Meteorology.vue")
        },

        {
            path: "/:pathMatch(.*)",
            component: () => import("@/views/Error/Error.vue")
        }
    ],

})
router.beforeEach((to,from)=>{
    // if(to.path!="/home/subhome"){
    //     return "/home/subhome"
    // }
    const token=localStorage.getItem("token")
    if(!localStorage.getItem("username")
        &&to.path!=="/home"
        &&to.path!=="/user/login"
        &&to.path!=="/user/register"){
        ElMessage.error('请先登录！')
        return "/user/login"
    }
})
export default router